<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let data = {
            name: 'zhangmeili',
            age: 18,
            hobby: [1, 2],
            school: {
                sname: '好谷'
            }
        };

        function observer(data) {
            if (typeof data === 'object') {
                Object.keys(data).forEach(key => {
                    defineReactive(data, key, data[key])
                })
            }
        };
        // Object.defineProperty压根就不支持数组的拦截。既然它不支持，那么我们只能拦截它的这些（'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'）改变自身数据的方法了。
        function defineReactive(obj, key, val) {
            observer(val)
                // console.log(data);
            Object.defineProperty(obj, key, {
                get() {
                    console.log('get')
                    return val
                },
                set(newVal) {
                    console.log('set')
                    if (newVal !== val) val = newVal
                }
            });
            console.log(data);
        };
        observer(data);


        // data.school.sname = '2323';
        data.hobby.push(23);
    </script>
</body>

</html>